//
//  SplashManager.m
//  JAGatherAdDemo
//
//  Created by Yc on 2024/4/7.
//

#import "SplashManager.h"
#import <JAGatherAdSDK/JAGatherAdSDK.h>
#import "Header.h"

@interface SplashManager ()<JAGAdSplashDelegate>

@property (nonatomic, strong) JAGAdSplash *splashAd;

@property (nonatomic, strong) UIImageView *launchView;

@property (nonatomic, strong) UIImageView *bottomView;

@property (nonatomic, assign) NSTimeInterval time;

@end

@implementation SplashManager

+ (instancetype)sharedManager {
    static SplashManager *manager = nil;
    static dispatch_once_t onceToken;
    dispatch_once(&onceToken, ^{
        manager = [[SplashManager alloc]init];
    });
    return manager;
}

- (instancetype)init {
    if (self = [super init]) {
        [self registerNotifications];
    }
    return self;
}

- (void)registerNotifications {
    [[NSNotificationCenter defaultCenter] addObserver:self selector:@selector(didEnterBackground) name:UIApplicationDidEnterBackgroundNotification object:nil];
    [[NSNotificationCenter defaultCenter] addObserver:self selector:@selector(enterForeground) name:UIApplicationWillEnterForegroundNotification object:nil];
}

- (void)loadSplashAd {
    BOOL state = [[NSUserDefaults standardUserDefaults] boolForKey:SplashBottomViewkey];
    self.splashAd.bottomView = state ? self.bottomView : nil;
    
    [self showLaunchView];
    [self.splashAd loadAd];
}

- (void)showLaunchView {
    [[UIApplication sharedApplication].keyWindow addSubview:self.launchView];
}

- (void)removeLaunchView {
    [self.launchView removeFromSuperview];
}

#pragma mark - NSNotification

- (void)didEnterBackground {
    self.time = CACurrentMediaTime();
}

- (void)enterForeground {
    
    BOOL state = [[NSUserDefaults standardUserDefaults] boolForKey:SplashHotStartkey];
    // 这里模拟简单的热启动广告，若需要屏蔽一些场景如推送消息不展示热启动广告的话则结合App生命周期补充相应业务逻辑。
    NSTimeInterval currentTime = CACurrentMediaTime();
    // 热启动时间间隔
    if (self.time > 0 && (currentTime - self.time) > 5 && state) {
        [self showLaunchView];
        [self loadSplashAd];
    }
}

#pragma mark - JAGAdSplashDelegate
/// 开屏广告请求成功
- (void)JAGatherSplashAdSuccessLoad:(JAGAdSplash *)splashAd {
    NSLog(@"%s", __FUNCTION__);
}

/// 开屏广告请求成功
/// @param isTimeout 加载是否超时
- (void)JAGatherSplashAdSuccessLoad:(JAGAdSplash *)splashAd timeout:(BOOL)isTimeout {
    NSLog(@"%s", __FUNCTION__);
    if (!isTimeout) {
        [self.splashAd showAd];
    } else {
        [self removeLaunchView];
    }
    
}

/// 开屏广告请求失败
- (void)JAGatherSplashAdFailLoad:(JAGAdSplash *)splashAd error:(NSError *)error {
    NSLog(@"%s", __FUNCTION__);
    [self removeLaunchView];
}

/// 开屏广告渲染失败
- (void)JAGatherSplashAdRenderFailed:(JAGAdSplash *)splashAd error:(NSError *)error {
    NSLog(@"%s", __FUNCTION__);
    [self removeLaunchView];
}

/// 开屏广告曝光回调
- (void)JAGatherSplashAdExposured:(JAGAdSplash *)splashAd {
    NSLog(@"%s", __FUNCTION__);
    [self removeLaunchView];
}

/// 开屏广告曝光失败
- (void)JAGatherSplashAdExposuredFailed:(JAGAdSplash *)splashAd error:(NSError *)error {
    NSLog(@"%s", __FUNCTION__);
    [self removeLaunchView];
}

/// 开屏广告点击回调
- (void)JAGatherSplashAdClicked:(JAGAdSplash *)splashAd {
    NSLog(@"%s", __FUNCTION__);
}

/// 开屏广告倒计时结束回调
- (void)JAGatherSplashAdCountdownToZero:(JAGAdSplash *)splashAd {
    NSLog(@"%s", __FUNCTION__);
}

/// 开屏广告点击跳过回调
- (void)JAGatherSplashAdSkiped:(JAGAdSplash *)splashAd {
    NSLog(@"%s", __FUNCTION__);
}

/// 开屏广告关闭回调
- (void)JAGatherSplashAdClosed:(JAGAdSplash *)splashAd {
    NSLog(@"%s", __FUNCTION__);
}

/// 开屏广告关闭落地页回调
- (void)JAGatherSplashAdCloseLandingPage:(JAGAdSplash *)splashAd {
    NSLog(@"%s", __FUNCTION__);
}

/// 广告加载超时
- (void)JAGatherSplashAdDidTimeout:(JAGAdSplash *)splashAd {
    NSLog(@"%s", __FUNCTION__);
    
}

#pragma mark - Lazy

- (JAGAdSplash *)splashAd {
    if (!_splashAd) {
        _splashAd = [[JAGAdSplash alloc] initWithPlacementID:splashID];
        _splashAd.delegate = self;
        _splashAd.viewController = [UIApplication sharedApplication].keyWindow.rootViewController;
    }
    return _splashAd;
}

- (UIImageView *)launchView {
    if (!_launchView) {
        _launchView = [[UIImageView alloc] initWithImage:[UIImage imageNamed:@"BgImage"]];
    }
    _launchView.frame = [UIScreen mainScreen].bounds;
    _launchView.userInteractionEnabled = YES;
    return _launchView;
}

- (UIImageView *)bottomView {
    if (!_bottomView) {
        _bottomView = [[UIImageView alloc] initWithFrame:CGRectMake(0, 0, [UIScreen mainScreen].bounds.size.width, 80)];
        _bottomView.image = [UIImage imageNamed:@"app_logo"];
    }
    return _bottomView;
}

@end
